package com.example.weather;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather.bean.WeatherInfo;
import com.example.weather.utils.WeatherUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_cityname)
    TextView tvCityname;
    @BindView(R.id.im_icon)
    ImageView imIcon;
    @BindView(R.id.tv_weather)
    TextView tvWeather;
    @BindView(R.id.tv_temp)
    TextView tvTemp;
    @BindView(R.id.tv_wind)
    TextView tvWind;
    @BindView(R.id.tv_pm)
    TextView tvPm;
    @BindView(R.id.btn_city1)
    Button btnCity1;
    @BindView(R.id.btn_city2)
    Button btnCity2;
    @BindView(R.id.btn_city3)
    Button btnCity3;
    private WeatherInfo weatherInfo;//解析后的天气对象
    ArrayList<WeatherInfo> weatherList;//解析后的天气列表
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //处理消息
            String data = (String) msg.obj;
            //解析data
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<WeatherInfo>>(){}.getType();
            weatherList = gson.fromJson(data,type);
            //初始化显示
            weatherInfo=weatherList.get(0);
            init();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定
        ButterKnife.bind(this);
        //找到控件，将解析后的数据，用控件展示出来
        //解析json文件，json数组，解析后的结果List
        //InputStream is = getResources().openRawResource(R.raw.weather);
        //weatherList = WeatherUtils.getWeatherList(is);
        //从服务器获取json文件，解析json列表，结果是一个天气列表
        //weatherList =WeatherUtils.getWeatherListFromServer();
        //设置初始的显示
        //weatherInfo = weatherList.get(0);
        //init();
        //将天气列表数据显示，点击按钮1-显示列表的第1个数据
        //点击按钮2-显示列表的第2个数据
        //点击按钮3-显示列表的第3个数据

        //使用handler
        WeatherUtils.getDataFromServer(handler);
        /*
        //工具类ButterKnife
        //1-找到控件
        //2-解析json文件，获取数据
        InputStream is = getResources().openRawResource(R.raw.weather);
        try {
            weatherInfo = WeatherUtils.getWeather(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3-设置控件的显示
        init();*/
    }

    private void init() {
        tvCityname.setText(weatherInfo.getName());
        tvWeather.setText(weatherInfo.getWeather());
        tvTemp.setText(weatherInfo.getTemp());
        tvWind.setText(weatherInfo.getWind());
        tvPm.setText(weatherInfo.getPm());
        //天气的图片  是根据天气来设置
        switch (weatherInfo.getWeather()) {
            case "晴":
                imIcon.setBackgroundResource(R.drawable.sun);
                break;
            case "多云":
                imIcon.setBackgroundResource(R.drawable.clouds);
                break;
            case "多云转晴":
                imIcon.setBackgroundResource(R.drawable.cloud_sun);
                break;
        }
    }

    @OnClick({R.id.btn_city1, R.id.btn_city2, R.id.btn_city3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_city1:
                weatherInfo = weatherList.get(0);
                init();
                break;
            case R.id.btn_city2:
                weatherInfo = weatherList.get(1);
                init();
                break;
            case R.id.btn_city3:
                weatherInfo = weatherList.get(2);
                init();
                break;
        }
    }
}
